package org.projectwithoutname.ui.wizard;

import java.util.Date;

import model.Image;
import model.Season;
import model.SeasonPackage;
import model.SeasonsPackSeason;
import model.Serie;
import model.SeriesPackSerie;
import model.SeriesPackage;
import model.Workspace;
import model.impl.ModelFactoryImpl;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.projectwithoutname.ui.pages.MainPage;
import org.projectwithoutname.ui.wizard.pages.InfoSeasonPage;
import org.projectwithoutname.ui.wizard.pages.NameImagePage;

public class NewSerieWizard extends Wizard implements INewWizard {
	IWorkbench workbench;
	IStructuredSelection selection;
	private MainPage mainPage;
	private NameImagePage nameImagePage;
	private InfoSeasonPage infoSeasonPage;
	private SeriesPackage seriePackage;
	private Workspace root;
	private SeriesPackSerie seriePack;
	public NewSerieWizard(MainPage mainPage, Workspace root){
		this.mainPage = mainPage;
		this.root = root;
	}
	
	
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.workbench = workbench;
		this.selection = selection;
	}
	
	@Override
	public void addPages() {
		nameImagePage = new NameImagePage("New Serie");
		addPage(nameImagePage);
		infoSeasonPage = new InfoSeasonPage("First Season");
		addPage(infoSeasonPage);
	}

	
	@Override
	public boolean performFinish() {
		mainPage.CreateButton(nameImagePage.getName(), nameImagePage.getImageSet());
		
//		create a new package if it doesn't exist
		if (root.getGroupOfSeries() == null)
			seriePackage = ModelFactoryImpl.eINSTANCE.createSeriesPackage();
		else 
			seriePackage = root.getGroupOfSeries();
		
//		create a new serie 
		seriePack = ModelFactoryImpl.eINSTANCE.createSeriesPackSerie();
		Serie serie = ModelFactoryImpl.eINSTANCE.createSerie();
		Image imagePath = ModelFactoryImpl.eINSTANCE.createImage();
		imagePath.setPath(nameImagePage.path());
		
		serie.setName(nameImagePage.getName());
		serie.setDescription(nameImagePage.getDescription());
		serie.getImages().add(imagePath);

//		create a seasons Pack
		SeasonPackage seasonPackage = ModelFactoryImpl.eINSTANCE.createSeasonPackage();
		serie.setAllSeason(seasonPackage);
		
//		create new seasons
		for (int i=0;i<1;i++){
			Season season = ModelFactoryImpl.eINSTANCE.createSeason();
			season.setName(infoSeasonPage.getName());
			season.setDescription(infoSeasonPage.getDescription());
			season.setPremier(convert(infoSeasonPage.getDatePremier()));
			season.setEnds(convert(infoSeasonPage.getDateEnd()));
			season.setComments(infoSeasonPage.getComments());
			
			SeasonsPackSeason sPackSeason = ModelFactoryImpl.eINSTANCE.createSeasonsPackSeason();
			sPackSeason.setSeason(season);
			
			seasonPackage.getAggregatedSeason().add(sPackSeason);
			root.getSeasons().add(season);
		}
		
		seriePack.setSerie(serie);
		seriePackage.getAggregatedSerie().add(seriePack);
		root.setGroupOfSeries(seriePackage);
		root.getSeries().add(serie);
		
		return true;
	}
	
	@SuppressWarnings("deprecation")
	public Date convert(DateTime date){
		Date d = new Date();
		d.setDate(date.getDay());
		d.setMonth(date.getMonth());
		d.setYear(date.getYear());
		return d;
	}

}
